package com.zf.nio.channel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

/**
 * @author Zhou Feng
 */
public class DatagramChannelDemo01 {
    public static void main(String[] args) throws IOException {
        DatagramChannel server = DatagramChannel.open();
        try (server){
            server.socket().bind(new InetSocketAddress(10086));
            //此例子是打开 10086 端口接收 UDP 数据包
            //2、通过 receive()接收 UDP 包
            ByteBuffer receiveBuffer = ByteBuffer.allocate(64);
            receiveBuffer.clear();
            SocketAddress receiveAddr = server.receive(receiveBuffer);
            System.out.println(receiveAddr.toString());
            System.out.println(new String(receiveBuffer.array()));
        }

    }
}
